home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / µSim 1.0.5 / source / TrackThumb.c < prev    next >
Encoding:
Text File  |  1995-11-01  |  2.0 KB  |  83 lines  |  [TEXT/CWIE]

  1. /*
  2. Copyright © 1993,1994,1995 Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware:
  5. you may copy, exchange, modify this code.
  6. You may include this code in any kind of application: freeware,
  7. shareware, or commercial, provided that full credits are given.
  8. You may not sell or distribute this code for profit.
  9. */
  10.  
  11. //#pragma load "MacDump"
  12.  
  13. #include    "TrackThumb.h"
  14.  
  15. //#pragma fourbyteints on
  16. //#pragma segment Main
  17.  
  18. /*
  19.     TrackThumb must be called as follows:
  20.     partCode = TrackThumb(whichControl, localPoint, actionProc);
  21.  
  22.     actionProc must be declared as follows:
  23.     pascal void actionProc(ControlHandle control, short value);
  24. */
  25.  
  26. short TrackThumb(ControlHandle control, Point start, void (*action)(ControlHandle, short))
  27. {
  28. enum {
  29. kTooDistant = 23
  30. };
  31.  
  32. EventRecord    dummyEventRec;
  33. Rect    rct;
  34. GrafPtr    savePort;
  35. Point    pos;
  36. long    newval;
  37. short    val, old;
  38. short    min, max;
  39. short    widthOrheight;
  40.  
  41. GetPort(&savePort);
  42. SetPort((*control)->contrlOwner);
  43. min    = GetControlMinimum(control);        /* min value */
  44. max    = GetControlMaximum(control);        /* max value */
  45. val    = GetControlValue(control);        /* cur value */
  46. rct    = (*control)->contrlRect;    /* control's rectangle */
  47.  
  48. widthOrheight = rct.right - rct.left;
  49. old = SHRT_MIN;
  50. do {
  51.     GetMouse(&pos);
  52.  
  53.     if (pos.v < rct.top + widthOrheight)
  54.         pos.v = rct.top + widthOrheight;
  55.     else if (pos.v > rct.bottom - widthOrheight)
  56.         pos.v = rct.bottom - widthOrheight;
  57.  
  58.     if ((pos.h - rct.right > kTooDistant) || (rct.left - pos.h > kTooDistant))
  59.         newval = val;
  60.     else {
  61.         newval = val + (max - min) * (long)(pos.v - start.v) / (rct.bottom - rct.top - widthOrheight - widthOrheight - widthOrheight);
  62.         if (newval < min)
  63.             newval = min;
  64.         else if (max < newval)
  65.             newval = max;
  66.         }
  67.  
  68.     if (newval != old) {
  69.         SetControlValue(control, newval);
  70.         action(control, old = GetControlValue(control));
  71.         }
  72.     else {    /* give some time to other applications */
  73.         SystemTask();
  74.         (void)EventAvail(everyEvent, &dummyEventRec);
  75.         }
  76.     }
  77. while(StillDown());
  78. SetPort(savePort);
  79. return kControlIndicatorPart;
  80. }
  81. //#pragma fourbyteints reset
  82.  
  83.